<?php

class Payroll Extends MY_Controller
{
	private $p_set; 
	private $emp_settings; 
	private $ps; // payroll settings
	
	private $employee_prof; // employee profile
	private $h_overtime; //holiday overtime in hours
	private $reg_overtime; //regular overtime in hours
	private $res_overtime;//restday overtime in hours
	
	private $total_overtime; // total overtime in pesos
	private $total_gross_salary; // total wage of employee without deductions
	private $withholding_tax; // withholding tax
	private $net_pay; // net pay
	
	/* Deductions */
	private $sss;
	private $philhealth;
	private $absences; // total absences in minutes
	private $undertime; //undertime in minutes
	
	public function __construct()
	{
		parent::__construct();
		$this->ps = $this->get_ps();
		$this->load->library(array('url_auth','form_validation'));
	}
	
	public function settings()
	{
		$this->view_data['system_message'] = $this->_msg();
		$this->load->model('M_payroll_settings','mps');
		$this->view_data['ps'] = $this->mps->get_mps();
		
		
		if($this->input->post('apply_settings'))
		{
			$this->form_validation->set_rules('wdim','Work Days','required');
			$this->form_validation->set_rules('whpd','Work Hours','required');
			$this->form_validation->set_rules('stoam','AM Time Out','required');
			$this->form_validation->set_rules('stopm','PM Time Out','required');
			$this->form_validation->set_rules('stipm','PM Time In','required');
			$this->form_validation->set_rules('stiam','AM Time Out','required');
			$this->form_validation->set_rules('overtime','Overtime Minute Calculation','required');
			$this->form_validation->set_rules('mwr','Minimum Wage Rate','required');
			
			
			if($this->form_validation->run() !== False)
			{
				if($this->mps->set_payroll_settings($this->input->post()))
				{
					$this->_msg('s','Settings Was Successfully Saved.','payroll/settings');
				}else
				{
					$this->view_data['system_message'] = $this->_msg('e','Unable to save settings.');
				}
			}
		}
	}
	
	public function index()
	{
	
		if(date('d') > 15)
		{
			$header = "For the month of ".date('F')." 16-30/31";
		}else{
			$header = "For the month of ".date('F')." 1-15";
		}
		
		$this->view_data['header'] = $header;
		$this->view_data['system_message'] = $this->_msg();
		$this->load->library(array('form_validation'));
		$this->load->helper('form');
		$this->load->model('M_employee','e');
		$this->view_data['auth'] = $this->url_auth->get_auth();
		$this->view_data['employee_opt'] = $this->e->get_employee_array();
	
		if($this->input->post('Generate_Payslip'))
		{
			if($this->set_settings($this->input->post('employee_id')))
			{
				$this->_generate_payroll($this->input->post('employee_id'));
			}else{
				$this->view_data['system_message'] = $this->_msg('e','Unable to generate payroll');
			}
		}
	}
	
	private function _generate_payroll($id)
	{
		$this->session->unset_userdata('payslip');
	
		$this->load->model('M_employee','e');
		$salary = $this->e->get('monthly_salary',$id);
		$this->employee_prof['sm_salary'] = ($salary->monthly_salary / 2);
		$this->employee_prof['m_salary'] = $salary->monthly_salary;
		
		$this->_compute_absences($id);
		$this->_compute_undertime($id);
		$this->_compute_overtime($id);
		$this->_compute_witholding_tax($id);
		$this->_compute_total_gross_salary();
		$this->_compute_net_salary();
		$this->_compute_sss();
		$this->_compute_philhealth();
		
		
		$session_data['payslip']['total_overtime'] = $this->total_overtime;
		$session_data['payslip']['absences'] = $this->absences;
		$session_data['payslip']['undertime'] = $this->undertime;
		$session_data['payslip']['total_gross_salary'] = $this->total_gross_salary;
		$session_data['payslip']['net_pay'] = $this->net_pay - $this->sss - $this->philhealth;
		$session_data['payslip']['monthly_salary'] = $this->employee_prof['m_salary'];
		$session_data['payslip']['smonthly_salary'] = $this->employee_prof['sm_salary'];
		$session_data['payslip']['employee_id'] = $id;
		$session_data['payslip']['withholding_tax'] = $this->withholding_tax;
		$session_data['payslip']['sss'] = $this->sss;
		$session_data['payslip']['phh'] = $this->philhealth;
		
		
		$this->session->set_userdata($session_data);
		redirect('payroll/view');
	}
	
	private function set_settings($id)
	{
		$this->load->model('M_employee','e');
		if($this->e->one_employee($id))
		{
			$this->emp_settings = $this->e->one_employee($id);
			return TRUE;
		}else{
			return FALSE;
		}
	}
	
	private function get_ps()
	{
		/*
			Set settings for
			- monthly salary
			- working days
			- 
		*/
		$this->load->model('M_payroll_settings','mps');
		return $this->mps->get_mps();
	}
	
	private function _compute_absences($id)
	{
		if(is_numeric($id))
		{
			$this->load->model('M_employee','me');
			$absences = $this->me->get_employee_absences($id);
			$total = ($this->employee_prof['sm_salary'] / $this->ps->wdim) * $absences;
			$this->absences= round($total,2);
		}
	}
	
	private function _compute_overtime($id)
	{
		if(is_numeric($id))
		{
			$this->load->model('M_employee','me');
			$hourly_wage_rate = round($this->ps->mwr / $this->ps->whpd,2);
			
			// get overtime for restday
			$restday = $this->me->get_all_restday_overtime($id,$hourly_wage_rate,$this->ps->stopm,$this->ps->overtime);
			if($restday)
			{
				$this->res_overtime = round((((($this->employee_prof['sm_salary'] / $this->ps->wdim) / $this->ps->whpd) * $restday) * .130),2);
			}else{
				$this->res_overtime = 0;
			}
			//overtime for regular days
			$regular = $this->me->get_all_regular_overtime($id,$hourly_wage_rate,$this->ps->stopm,$this->ps->overtime);
			if($regular)
			{
				$this->reg_overtime = round((((($this->employee_prof['sm_salary'] / $this->ps->wdim) / $this->ps->whpd) * $regular) * .125),2);
			}else{
				$this->reg_overtime = 0;
			}
			// overtime for holidays
			$holiday = $this->me->get_all_holiday_overtime($id,$hourly_wage_rate,$this->ps->stopm,$this->ps->overtime);
			if($holiday){
				$this->h_overtime = round((((($this->employee_prof['sm_salary'] / $this->ps->wdim) / $this->ps->whpd) * $holiday) * .130),2);
			}else{
				$this->h_overtime = 0;
			}
			
			$this->total_overtime = round($this->res_overtime + $this->reg_overtime + $this->h_overtime,2);
		}else{
			return FALSE;
		}
	}
	
	
	private function _compute_undertime($id)
	{
		if(is_numeric($id))
		{
			$this->load->model('M_employee','me');
			$undertime_min = $this->me->get_all_undertime($id,$this->ps->stopm);
			$this->undertime =  round((((($this->employee_prof['sm_salary'] / $this->ps->wdim) / $this->ps->whpd) / 60 )* $undertime_min),2);
		}else{
			return FALSE;
		}
	}
	
	private function _compute_witholding_tax($id)
	{
		if(is_numeric($id))
		{
			$this->load->model('M_employee','me');
			$this->withholding_tax = $this->me->compute_withholding_tax($this->employee_prof['sm_salary'],$id);
		}else{
			return FALSE;
		}
	}
	
	private function _compute_total_gross_salary()
	{
		$this->total_gross_salary = round(((($this->employee_prof['sm_salary'] - $this->absences) - $this->undertime) + $this->total_overtime),2);
	}
	
	private function _compute_net_salary()
	{
		$this->net_pay = $this->total_gross_salary - $this->withholding_tax;
	}
	public function view()
	{
		if($this->session->userdata('payslip'))
		{
			$data = (object)$this->session->userdata('payslip');
			$this->load->model('M_employee','me');
			$this->view_data['ep'] = $this->me->one_employee($data->employee_id);
			$this->view_data['semi_monthly_salary'] = $data->smonthly_salary;
			$this->view_data['monthly_salary'] = $data->monthly_salary;
			$this->view_data['total_overtime'] = $data->total_overtime;
			$this->view_data['absences'] = $data->absences;
			$this->view_data['total_gross_salary'] = $data->total_gross_salary;
			$this->view_data['total_net_pay'] = $data->net_pay;
			$this->view_data['withholding_tax'] = $data->withholding_tax;
			$this->view_data['undertime'] = $data->undertime;
			$this->view_data['sss'] = $data->sss;
			$this->view_data['phh'] = $data->phh;
		}
	}
	
	public function printpayslip()
	{
	
		$this->load->library(array('mpdf'));
		

		if($this->session->userdata('payslip'))
		{
			$data = (object)$this->session->userdata('payslip');
			$this->load->model('M_employee','me');
			$emp_data['ep'] = $this->me->one_employee($data->employee_id);
			$emp_data['semi_monthly_salary'] = $data->smonthly_salary;
			$emp_data['monthly_salary'] = $data->monthly_salary;
			$emp_data['total_overtime'] = $data->total_overtime;
			$emp_data['absences'] = $data->absences;
			$emp_data['total_gross_salary'] = $data->total_gross_salary;
			$emp_data['total_net_pay'] = $data->net_pay;
			$emp_data['withholding_tax'] = $data->withholding_tax;
			$emp_data['undertime'] = $data->undertime;
			$emp_data['sss'] = $data->sss;
			$emp_data['phh'] = $data->phh;
		
			$html = $this->load->view('print/payslip',$emp_data,TRUE);
			$filename = str_replace(' ','_',trim('studentname')).'_'.trim('idnumber');
			$mpdf=new mPDF('c','A4','','' , 10 , 10 , 10 , 10 , 0 , 0);
			$mpdf->SetDisplayMode('fullpage');
			$mpdf->list_indent_first_level = 0;  // 1 or 0 - whether to indent the first level of a list
			$mpdf->WriteHTML($html);
			$mpdf->Output($filename,'I');
			
		}else{
			redirect('payroll/view');
		}
	
	
	}
	
	private function _compute_sss()
	{
		if(date('j',time()) >= '30' AND date('j',time()) <= '31')
		{
			$this->load->model('M_payroll_settings','mps');
			$this->sss = $this->mps->get_data_for_salary_sss($this->total_gross_salary);
		}else{
			$this->sss = 0;
		}
	}
	
	private function _compute_philhealth()
	{
		if(date('j',time()) >= '30' AND date('j',time()) <= '31')
		{
			$this->load->model('M_payroll_settings','mps');
			$this->philhealth = $this->mps->get_data_for_salary_philhealth($this->total_gross_salary);
		}else{
			$this->philhealth = 0;
		}
	}
}